<?php
/**
 * Created by PhpStorm.
 * userValidate: gh
 * Date: 2018/5/17
 * Time: 9:59
 */

namespace app\firm\behavior;


use redis\Redis;
use think\Config;
use think\Db;
use think\Session;
use think\Request;
use app\firm\model\ProjectUser;

class Behavior
{
    
    public function run(&$params)
    {
        $filter = \config('Behavior.filter');
        $filter = array_map("strtolower", $filter);
        $request = request();
        $res_url = $request->dispatch()['module'];
        $bhv = $res_url[0] . '/' . $res_url[1] .'/' . $res_url[2];
        if(in_array(strtolower($bhv) , $filter) == true){
            $token = $request->header('token');
            if ($token) {
                $redis = Redis::instance();
                $firm_info = json_decode($redis->hGet('firm_info' , $token) ,true);
                file_put_contents('behaivor.txt', json_encode($firm_info,JSON_UNESCAPED_UNICODE)  . PHP_EOL, FILE_APPEND);
                if ($firm_info) {
                    $token = $redis->get('user:' . $firm_info['id']);
                    file_put_contents('behaivor.txt', $token  . PHP_EOL, FILE_APPEND);
                    if(!$token){
                        abort(json([
                            'msg' => '账号过期请重新登录',
                            'status' => 202
                        ]));
                    }
                    $status = Session::get('user_id');
                    if(empty($status)){
                        abort(json([
                            'msg' => '账号异常请重新登录!',
                            'status' => 202
                        ]));
                    }
                    if($firm_info['modify'] == true){
                        $redis->hDel('firm_info', $firm_info['token']);
                        $projectUser = new ProjectUser();
                        $firm_info = $projectUser->getUserInfo(['token'=>$firm_info['token']]);
                        $redis->hSet('firm_info', $token, json_encode($firm_info));
                        $redis->set('user:' . $firm_info['id'], $token);
                    }
                    file_put_contents('abehaivor.txt', json_encode($firm_info,JSON_UNESCAPED_UNICODE)  . PHP_EOL, FILE_APPEND);
                    if($firm_info['status'] != 5){
                        $arr = \config('Behavior.notIn');
                        $arr = array_map("strtolower", $arr);
                        if(in_array(strtolower($bhv) , $arr) == false){
                            abort(json([
                                'msg' => '资料审核未通过，请等待审核通过',
                                'status' => 203
                            ]));
                        }
                    }
                    $request->firm_info = $firm_info;
                } else {
                    abort(json([
                        'msg' => '账号异常请重新登录',
                        'status' => 202
                    ]));
                }
            } else {
                abort(json([
                    'msg' => '请登录',
                    'status' => 201
                ]));
            }
        }
    }

}